package com.ruoyi.project.easyexcel;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.StyleProperty;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author huyunqiang
 * @Description 自定义表头和内容样式
 * @Date 2023/6/30 17:31
 **/
public class XCellStyle extends AbstractVerticalCellStyleStrategy {

    //key为ExcelProperty value为颜色
    public Map<Integer,Short> colorMap=new HashMap<>();

    public XCellStyle(Map<Integer,Short> colorMap){
        this.colorMap=colorMap;
    }

    public XCellStyle() {

    }

    //表格头样式
    @Override
    protected WriteCellStyle headCellStyle(Head head) {
        WriteCellStyle writeCellStyle=new WriteCellStyle();
        WriteFont font=new WriteFont();

//        //自定义字体颜色
//        for(Integer key:colorMap.keySet()){
//            if(head.getColumnIndex()==key){
//                font.setColor(colorMap.get(key));
//                break;
//            }else {
//                font.setColor(IndexedColors.BLACK.index);//默认颜色
//            }
//        }
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.NONE);
        //有兴趣可以自己定义别的这里只定义了color
        writeCellStyle.setWriteFont(font);
        // 字体
        font.setFontName("宋体");
        // 字体大小
        font.setFontHeightInPoints((short) 14);
        // 设置加粗
        font.setBold(true);
        writeCellStyle.setWriteFont(font);
        // 设置文字左右居中
        // 设置文字左右居中
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        // 设置文字上下居中
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return writeCellStyle;
    }

    @Override
    protected WriteCellStyle contentCellStyle(Head head) {


        WriteCellStyle writeCellStyle=new WriteCellStyle();
        WriteFont font=new WriteFont();

        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.NONE);
        //有兴趣可以自己定义别的这里只定义了color
        writeCellStyle.setWriteFont(font);
        // 字体
        font.setFontName("宋体");
        // 字体大小
        font.setFontHeightInPoints((short) 9);
        // 设置加粗
        font.setBold(false);


        font.setColor(IndexedColors.BLACK.index);
        String fieldName = head.getFieldName();


//        colorMap.put(1, (short) 255);
//        colorMap.put(2, (short) 255);
//        colorMap.put(3, (short) 255);
//        colorMap.put(4, (short) 255);
//        for(Integer key:colorMap.keySet()){
//            if(head.getColumnIndex()==key){
//                font.setColor(IndexedColors.RED1.index);
//                break;
//            }else {
//                font.setColor(IndexedColors.BLACK.index);//默认颜色
//            }
//
//        }


        writeCellStyle.setWriteFont(font);
        // 设置文字左右居中
        // 设置文字左右居中
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        // 设置文字上下居中
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 自动换行
        writeCellStyle.setWrapped(true);
        return writeCellStyle;
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {

        super.setContentCellStyle(cell, head, relativeRowIndex);
    }
}
